import sublime
import sublime_plugin

import os, sys

def plugin_loaded():
	print("plugin_loaded")
	settings = sublime.load_settings("wlgame-plugin.sublime-settings")

	root = os.path.join(sublime.packages_path(), settings.get("project_name"))
	sys.path.append(root)

def plugin_unloaded():
	print("plugin_unloaded")

class wlgame_plugin(sublime_plugin.EventListener):

	def on_window_command (self, window, command_name, args):
		print(command_name)

	def on_post_text_command(self, view, command_name, args):
		if command_name != "insert":
			return 

		isLuaFile = view.file_name().endswith(".lua")
		if not isLuaFile:
			return
					
		selRegion = view.sel()[0]
		pos = view.rowcol(selRegion.begin())
		point = view.text_point(pos[0] - 1, pos[1])
		selRegion = view.line(point)
		line = view.line(selRegion)
		content = view.substr(line).replace(" ", "").replace("\t", "")
		print(content)
		if content.replace(" ", "").startswith("---"):
			print(pos[0])
			if pos[0]-1 == 0:
				view.run_command("wlgame_insert_lua_header")
			else:
				view.run_command("wlgame_insert_lua_func_comment")
